(function(){
    var win = window,
        doc =document,
        sendurl = 'http://127.0.0.1/';

    EcJS = {
        setCookie:function(key,value,options){
            options || (options = {})
            if (options.encode) value = encodeURIComponent(value);
            if (options.domain) value += '; domain=' + options.domain;
            if (options.path) value += '; path=' + options.path;
            if (options.duration){
                var date = new Date();
                date.setTime(date.getTime() + options.duration * 24 * 60 * 60 * 1000);
                value += '; expires=' + date.toGMTString();
            }
            if (options.secure) value += '; secure';
            document.cookie = key + '=' + value;
        },
        escapeRegExp:function(str){
            return str.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
        },
        getCookie:function(key){
            var value = document.cookie.match('(?:^|;)\\s*' + this.escapeRegExp(key) + '=([^;]*)');
            return (value) ? decodeURIComponent(value[1]) : null;
        },
        disposeCookie:function(key,options){
            options || (options = {})
            options.duration  = -1;
            this.setCookie(key,'',options);
        },
        getBrowser:function(){
            var ua = navigator.userAgent.toLowerCase(),
                platform = navigator.platform.toLowerCase(),
                UA = ua.match(/(opera|ie|firefox|chrome|mqqbrowser|version)[\s\/:]([\w\d\.]+)?.*?(safari|version[\s\/:]([\w\d\.]+)|$)/) || [null, 'unknown', 0],
                mode = UA[1] == 'ie' && document.documentMode,

                nav = navigator.plugins['Shockwave Flash'], version;
                try{
                    version = nav ? nav.description: new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
                }catch(e){
                }

            version = (version||'0 r0').match(/\d+/g);

            var flash = Number(version[0] || '0.' + version[1]) || 0,
                name = (UA[1] == 'version') ? UA[3] : UA[1],
                platform = ua.match(/ip(?:ad|od|hone)/) ? 'ios' : (ua.match(/(?:webos|android)/) || platform.match(/mac|win|linux/) || ['other'])[0],
                browser =  mode || parseFloat((UA[1] == 'opera' && UA[4]) ? UA[4] : UA[2]), 
                winScreen = win.screen,
                screen = winScreen ? winScreen.width + "x" + winScreen.height:'-';
                colorDepth = winScreen ? winScreen.colorDepth + "-bit": '-';
                javaEnabled = navigator&&  navigator.javaEnabled() ? 1 : 0;

            return '&sfv='+ flash +'&sbi='+name +'&platform='+platform +'&browser='+browser
                   +'&ssc='+screen+'&scd='+colorDepth+'&sja='+javaEnabled;

            return {
                Flash:{
                    version: Number(version[0] || '0.' + version[1]) || 0,
                    build: Number(version[2]) || 0
                },
                name: (UA[1] == 'version') ? UA[3] : UA[1],
                version: mode || parseFloat((UA[1] == 'opera' && UA[4]) ? UA[4] : UA[2]),
                Platform: {
                    name: ua.match(/ip(?:ad|od|hone)/) ? 'ios' : (ua.match(/(?:webos|android)/) || platform.match(/mac|win|linux/) || ['other'])[0]
                }
            };
        },
        init:function(url,param,callback){

        }
    };
    var bind = function(obj) {
        return function(name, aO, fn) {
            obj[name] = function() {
                return fn.apply(obj, arguments)
            };
            return  fn;
        }
    };

    function Send(){
        var _this = this;
    };
    Send.prototype = {  
        send:function(url){
            this.sendByImage(url);
        },
        sendByImage :function(src,fn){
            var img = new Image(1, 1);
            img.onload = function() {
                img.onload = null;
                img.onerror = null;
                fn && fn();
            };
            img.onerror = function() {
                img.onload = null;
                img.onerror = null;
                fn && fn();
            }
            img.src =src 
        }
    }; 

    var data = {},dataStr='', dataList = [];

    function EcData(){
        var _this = this;
        _this.referrer = doc.referrer;

        function isValue(key, a5) {
            var temp = a5;
            while (key[temp]) {
                temp = temp + " "
            }
            return temp; 
        }
        function addParam(name, key, a7, fn, fns) {
            var a5 = a5 || "|";
            _this[name] = function() {
                if (fn) {
                    if (fn.apply(window, arguments) === true) {
                        return
                    }
                }
                var bf = [];
                var bb = fns? fns.apply(window, arguments) : arguments;
                for (var bd = 0, ba = bb.length; bd < ba; bd++) {
                    try {
                        bf[bd] = bb[bd] == null ? "0": (bb[bd] + "").replace(/\|/g, " ")
                    } catch(bg) {}
                }
                var be = bf.join(a5);
                var bc = isValue(data, key);
                dataList.push(bc);
            
                data[bc] = encodeURIComponent(be)
            }
        }
        addParam("$appendBrand", "qzjbr");
        addParam("$appendCat", "qzjca");
        addParam("$appendGoods", "qzjgo");
        addParam("$appendCartItem", "qzjcag", 0);
        addParam("$appendCartPac", "qzjcap", 0);
        addParam("$appendCartPacItem", "qzjcapg", 0);
        addParam("$appendOrder", "qzjor", 0, null,
        function(ba) {

        });
        addParam("$appendOrderPac", "qzjorp", 0);
        addParam("$appendOrderPacGoods", "qzjorpg", 0);
        addParam("$appendItem", "qzjorg", 0, null, null,
        function() {
            
        });
        addParam("$appendSearchResult", "qzjse");
        addParam("$appendCompareItem", "qzjcog", 0);
        addParam("$appendFavItem", "qzjgof", 0);
        addParam("$appendGift", "qzjgi", 0);
        addParam("$appendHistory", "qzjorg", 0);
        addParam("$appendOrderDetail", "qzjord");
        addParam("$appendRecentPurchase", "qzjreog");

    };
    EcData.prototype = {
        send:function(param){
            var src = sendurl;
                src = src + '?'+ (param ? param : '');
            var url = src + this.getPageInfo();

            var request = new Send();
            request.send(url);
        },
        getPageInfo:function(){
            var ba = dataStr;
            ba += "&spt=" + document.title ;
            var browser = EcJS.getBrowser();        
            ba += browser;
            return ba;
        },
        $regAction:function(){
            var reg_num = "reg_num";
            var value = EcJS.getCookie(reg_num);
            if(value != null){
                value++;
            }else{
                value=1;
            }
            EcJS.setCookie(reg_num,value);
            this.send('&qzjregc=1');
        },
        $setSiteId:function(siteId){
            siteId = siteId ? siteId:'';
            dataStr += '&ssi='+siteId;
        },
        $setCommon:function(pageType, pageId, userName, userId){
            pageType = pageType||pageType===null ? pageType:'';
            pageId = pageId||pageId===null ? pageId:'';
            userName = userId||userName===null ? userName:'';
            userId = userId||userId===null ? userId:'';
            dataStr += '&spty='+pageType+'&spi='+pageId+'&sun='+userName+'&sui='+userId;
        },
        _logConversion:function(){
        //  this.initData();

            this.send();
        },
        _logData:function() {
            try {
                var arr = [];

                for (var i = 0, j = dataList.length; i < j ; i++) {
                    var temp = dataList[i],
                        dt = data[temp];
                    dt && arr.push(temp + "=" + dt)
                }
                var  param = arr.join("&");
                this.send(param);
             }catch(e) {}
        }
    };

    function Tacker (){
        var _this = this, _bind = bind(this);
        _this.push = function(){
        
            for (var param = arguments, i= 0, j= 0; i < param['length']; i++) {
                try {
                    if (typeof param[i] === "function") {
                        param[i]();
                    } else {
                        var temp = "", key = param[i][0];
                        
                        obj = new EcData();
                    
                        obj[key].apply(obj, param[i].slice(1))
                    }
                } catch(e){
                    j++
                }
            }
            return j; 
        }
    };


    var tacker = new Tacker();
    window['_ecq'] = tacker;

    // ba.initData();

    



})();
